# -*- python -*-
# Copyright (c) 2012 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

Import('env')

# TODO(bradnelson): Enable for PNaCl (figure out a way to get full frames
# with PNaCl).
#
# This test is disabled under ASan because SConstruct disables the
# CommandSelLdrTestNacl() half of the test implicitly based on its use
# of declares_exit_status=True.
is_broken = (env.Bit('bitcode') or
             env.Bit('running_on_valgrind') or
             env.Bit('asan'))

# Keep frame pointers so we get a nice stack trace.
# Set -O0 as otherwise some of the layers in the test case get optimized away.
env.Append(CCFLAGS=['-O0', '-fno-omit-frame-pointer', '-g'])
env['COMPONENT_STATIC'] = not env.Bit('nacl_glibc')
env['ENV']['NACL_UNTRUSTED_EXCEPTION_HANDLING'] = '1'

untrusted_crash_dump_lib = env.ComponentLibrary(
    'untrusted_crash_dump_lib',
    'untrusted_crash_dump_lib.c',
    EXTRA_LIBS=['${PTHREAD_LIBS}', '${NONIRT_LIBS}'])

if env.Bit('tests_use_irt'):
  untrusted_crash_dump_test = env.ComponentProgram(
      'untrusted_crash_dump_test',
      'untrusted_crash_dump_test.c',
      EXTRA_LIBS=['untrusted_crash_dump_lib',
                  'untrusted_crash_dump',
                  '${PTHREAD_LIBS}', '${NONIRT_LIBS}'])

  dump_file = env.File('untrusted_crash_dump_test_core.json')
  run_test = env.CommandSelLdrTestNacl(
      'untrusted_crash_dump_test_run.out',
      untrusted_crash_dump_test,
      declares_exit_status=True,
      sel_ldr_flags=['-a', '-E', 'NACLCOREFILE=' + dump_file.abspath])
  if run_test:
    env.AlwaysBuild(run_test)
    env.SideEffect(dump_file, run_test)
    node = env.Command('untrusted_crash_dump_test.out',
                       ['untrusted_crash_dump_test.py',
                        dump_file, untrusted_crash_dump_test],
                       '${PYTHON} ${SOURCES} - ${ADDR2LINE} '
                       '${LIB_DIR} ${NACL_SDK_LIB} ${TARGET_FULLARCH}')
    env.AddNodeToTestSuite(
        node, ['small_tests'],
        'run_untrusted_crash_dump_test', is_broken=is_broken)
